angular.module('starter.controllers')
.controller('MyticketCtrl', function($scope, museumFac, $stateParams, $timeout, $ionicScrollDelegate, cfg, $location, $cookies, $interval) {
  	$scope.change_language = function(id){
      $scope.language = id;
      var expireDate = new Date();  
      expireDate.setDate(expireDate.getDate() + 30);                
      $cookies.put('language', id, {'expires': expireDate.toUTCString()});
      location.reload();
      if(id == 0){
        document.getElementById('title').innerHTML = '提示';
        document.getElementById('windowclosebutton').value = '确定';
      }else{
        document.getElementById('title').innerHTML = 'Hint';
        document.getElementById('windowclosebutton').value = 'Confirm';
      }

    }

    $scope.getLanguage = function(){
      $scope.language = $cookies.get('language');
      if($scope.language){
          $scope.language = $scope.language;
      }else{
          $scope.language = 0;
      }
      if($scope.language == 0){
        document.getElementById('title').innerHTML = '提示';
        document.getElementById('windowclosebutton').value = '确定';
      }else{
        document.getElementById('title').innerHTML = 'Hint';
        document.getElementById('windowclosebutton').value = 'Confirm';
      }
    }
    $scope.getLanguage();

    $scope.getOrder = function(){
    	museumFac.user.codes(function(data){
        	$scope.orders = data.info;

          angular.forEach($scope.orders, function(list){
            angular.forEach(list.codes, function(list1){
              if(list1.status == 0){
                list.is_use = 0; 
              }
            });
          });

      	},function(data){
        	alert(data.data.info);
    	});
  	}
  	$scope.getOrder();

    $scope.getnewticket = function(id){
      museumFac.user.codes(function(data){
          $scope.neworders = data.info;

          angular.forEach($scope.neworders, function(list){
            if(list.id = id){
              angular.forEach(list.codes, function(list1){
                if(list1.status == 1){
                  angular.forEach($scope.codes, function(list2){
                    if(list1.id == list2.id){
                      list2.status = 1;
                    }
                  });
                }
              });
            }
            
          });

        },function(data){
          alert(data.data.info);
      });
    }

    $scope.cancle_show = function(){
      $scope.show_box = 0;
    }

    $scope.ticketrush = function(id){

      $interval(function() {
        $scope.getnewticket(id);
      }, 2000);

    }

  	$scope.show_box = 0;
  	$scope.show = function(list){
      $scope.mubu_mark = 1;
  		$scope.show_box = 1;
  		$scope.codes = list.codes;
  		$scope.order = list;
  		$timeout(function() {
  			var mySwiper = new Swiper('.swiperbox'+list.id + ' '+ '.swiper-container',{
          initialSlide :0,
  			  pagination : '.swiperbox'+list.id + ' ' + '.swiper-pagination',
          paginationType : 'fraction',
  			  //pagination : '#swiper-pagination1',
  			})
  		}, 50);

      $timeout(function() {
        angular.forEach(list.codes, function(item){

          JsBarcode("#barcode"+item.id, item.redeem_code, {
            format: "CODE128",
            displayValue: false,
          });

        });
      }, 100);

      $ionicScrollDelegate.$getByHandle('mainScroll').resize();

      // 每两秒更新门票状态
      $scope.ticketrush(list.id);
  	}

    $scope.show1 = function(list){
      $scope.mubu_mark = 2;
      $scope.show_box = 1;
      $scope.order = list;
      $timeout(function() {
        var mySwiper = new Swiper('.swiperbox'+list.id + ' '+ '.swiper-container',{
          initialSlide :0,
          pagination : '.swiperbox'+list.id + ' ' + '.swiper-pagination',
          paginationType : 'fraction',
          //pagination : '#swiper-pagination1',
        })
      }, 50);

      $timeout(function() {

          JsBarcode("#barcode"+list.order_num, list.order_num, {
            format: "CODE128",
            displayValue: false,
          });

      }, 100);

      $ionicScrollDelegate.$getByHandle('mainScroll').resize();
    }

    $scope.backhome = function(){
      $location.url('home');
    }

    $scope.checkOpenid = function(){
      museumFac.user.usertest(function(data){
            $scope.is_login = data.info;
          },function(data){
            alert(data.data.info);
        });
    }
    $scope.checkOpenid();

    $scope.gotoCenter = function(){
      museumFac.user.usertest(function(data){
        if(data.info == true){
          $location.url('center');
        }else{
          $scope.url = window.location.href;
          $cookies.put('lastPage', $scope.url);
          $location.url('login');
        }
      });
    }

    $scope.cancelOrder = function(list){
      if(confirm('您是否确认取消本次预约？')){
        museumFac.ticket.cancelticketorder({
          order_id:list.id
        },function(data){
          alert('取消成功');
          $scope.getOrder();
        },function(data){
          alert(data.data.info);
        });
      }
    }

    // 每十秒更新
    // $scope.pinglunrush = function(){

    //   $interval(function() {
    //     $scope.getOrder();
    //   }, 2000);

    // }
    // $scope.pinglunrush();
})